package com.situ.ymall.mapper;

import com.situ.ymall.entity.Shopcart;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.boot.autoconfigure.data.ConditionalOnRepositoryType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface ShopcartMapper {
    @Delete({
        "delete from shopcart",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into shopcart (user_id, ",
        "goods_id, count)",
        "values (#{userId,jdbcType=INTEGER}, ",
        "#{goodsId,jdbcType=INTEGER}, #{count,jdbcType=INTEGER})"
    })
    int insert(Shopcart record);

    @Select({
        "select",
        "id, user_id, goods_id, count",
        "from shopcart",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @ResultMap("shopCart")  // 引用另一个id为shopCart的Result
    Shopcart selectByPrimaryKey(Integer id);

    @Select({
        "select",
        "id, user_id, goods_id, count",
        "from shopcart"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
        @Result(column="goods_id", property="goodsId", jdbcType=JdbcType.INTEGER),
        @Result(column="count", property="count", jdbcType=JdbcType.INTEGER)
    })
    List<Shopcart> selectAll();

    @Update({
        "update shopcart",
        "set count = #{count,jdbcType=INTEGER}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Shopcart record);


    /**
     * 根据商品id和用户ID来查询购物车
     * @param cart
     * @return
     */
    @Select({
            "select * from shopcart where user_id=#{userId} and goods_id = #{goodsId}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
            @Result(column="goods_id", property="goodsId", jdbcType=JdbcType.INTEGER),
            @Result(column="count", property="count", jdbcType=JdbcType.INTEGER)
    })
    Shopcart select(Shopcart cart);

    @Select({
            "select * from shopcart where user_id = #{userId}"
    })
    @Results(id = "shopCart",value = {
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
            @Result(column="goods_id", property="goodsId", jdbcType=JdbcType.INTEGER),
            @Result(column="count", property="count", jdbcType=JdbcType.INTEGER),
            @Result(column = "goods_id" ,property = "goods",
                    one = @One(select = "com.situ.ymall.mapper.GoodsMapper.selectByPrimaryKey") )
    })
    List<Shopcart> selectByUserId(Integer userId);
}